CREATE TABLE RegionAudit(
    operacion           char(6)   NOT NULL,
    fecha               timestamp NOT NULL,
    idUsuario           text      NOT NULL,
    idregion            integer   NOT NULL,
    nombre		character varying(45)
);

CREATE OR REPLACE FUNCTION funAuditarRegion() RETURNS TRIGGER AS $RegionAudit$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO RegionAudit SELECT 'Delete', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO RegionAudit SELECT 'Update', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO RegionAudit SELECT 'Insert', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; 
    END;
$RegionAudit$ LANGUAGE plpgsql;

CREATE TRIGGER AuditoriaRegion
AFTER INSERT OR UPDATE OR DELETE ON Region
FOR EACH ROW EXECUTE PROCEDURE funAuditarRegion();

CREATE TABLE ShippingAudit(
    operacion           char(6)   NOT NULL,
    fecha               timestamp NOT NULL,
    idUsuario           text      NOT NULL,
    idShipping          integer   NOT NULL,
    nombre		character varying(45),
    costo               float4,
    tipo                varchar(45),
    idRegion            int
);

CREATE OR REPLACE FUNCTION funAuditarShipping() RETURNS TRIGGER AS $ShippingAudit$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO ShippingAudit SELECT 'Delete', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO ShippingAudit SELECT 'Update', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO ShippingAudit SELECT 'Insert', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; 
    END;
$ShippingAudit$ LANGUAGE plpgsql;

CREATE TRIGGER AuditoriaRolUsuario
AFTER INSERT OR UPDATE OR DELETE ON RolUsuario
FOR EACH ROW EXECUTE PROCEDURE funAuditarRolUsuario();

CREATE TABLE RolUsuarioAudit(
    operacion           char(6)   NOT NULL,
    fecha               timestamp NOT NULL,
    idUsuario           text      NOT NULL,
    idRolUsuario        integer   NOT NULL,
    titulo 		varchar(45)
);

CREATE OR REPLACE FUNCTION funAuditarRolUsuario() RETURNS TRIGGER AS $RolUsuarioAudit$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO RolUsuarioAudit SELECT 'Delete', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO RolUsuarioAudit SELECT 'Update', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO RolUsuarioAudit SELECT 'Insert', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; 
    END;
$RolUsuarioAudit$ LANGUAGE plpgsql;

CREATE TRIGGER AuditoriaRolUsuario
AFTER INSERT OR UPDATE OR DELETE ON RolUsuario
FOR EACH ROW EXECUTE PROCEDURE funAuditarRolUsuario();

CREATE TABLE UsuarioAudit(
    operacion           char(6)   NOT NULL,
    fecha               timestamp NOT NULL,
    idUsuarioA          text      NOT NULL,
    idUsuario           integer   NOT NULL,
    nombre		varchar(45),
    nickname            varchar(45),
    email               varchar(50),
    password            varchar(45),
    cuentaActiva        boolean,
    idRolUsuario        int
);

CREATE OR REPLACE FUNCTION funAuditarUsuario() RETURNS TRIGGER AS $UsuarioAudit$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO UsuarioAudit SELECT 'Delete', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO UsuarioAudit SELECT 'Update', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO UsuarioAudit SELECT 'Insert', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; 
    END;
$UsuarioAudit$ LANGUAGE plpgsql;

CREATE TRIGGER AuditoriaUsuario
AFTER INSERT OR UPDATE OR DELETE ON Usuario
FOR EACH ROW EXECUTE PROCEDURE funAuditarUsuario();

CREATE TABLE PedidoAudit(
    operacion           char(6)   NOT NULL,
    fecha               timestamp NOT NULL,
    idUsuarioA          text      NOT NULL,
    idPedido            integer   NOT NULL,
    statusOrden         varchar(45),
    fechaIngreso        timestamp,
    descripcion         varchar(100),
    fechaPago           timestamp,
    statusPago          boolean,
    idUsuario           int,
    idShipping          int
);

CREATE OR REPLACE FUNCTION funAuditarPedido() RETURNS TRIGGER AS $PedidoAudit$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO PedidoAudit SELECT 'Delete', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO PedidoAudit SELECT 'Update', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO PedidoAudit SELECT 'Insert', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; 
    END;
$PedidoAudit$ LANGUAGE plpgsql;

CREATE TRIGGER AuditoriaPedido
AFTER INSERT OR UPDATE OR DELETE ON Pedido
FOR EACH ROW EXECUTE PROCEDURE funAuditarPedido();

CREATE TABLE RatingAudit(
    operacion           char(6)   NOT NULL,
    fecha               timestamp NOT NULL,
    idUsuarioA          text      NOT NULL,
    idRating            integer   NOT NULL,
    estrellas           int,
    idUsuario           int,
    rating              int
);

CREATE OR REPLACE FUNCTION funAuditarRating() RETURNS TRIGGER AS $RatingAudit$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO RatingAudit SELECT 'Delete', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO RatingAudit SELECT 'Update', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO RatingAudit SELECT 'Insert', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; 
    END;
$RatingAudit$ LANGUAGE plpgsql;

CREATE TRIGGER AuditoriaRating
AFTER INSERT OR UPDATE OR DELETE ON Rating
FOR EACH ROW EXECUTE PROCEDURE funAuditarRating();

CREATE TABLE CategoriaAudit(
    operacion           char(6)   NOT NULL,
    fecha               timestamp NOT NULL,
    idUsuarioA          text      NOT NULL,
    idCategoria		int,
    titulo              varchar(45),
    Categoria_idCategoria int
);

CREATE OR REPLACE FUNCTION funAuditarCategoria() RETURNS TRIGGER AS $CategoriaAudit$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO CategoriaAudit SELECT 'Delete', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO CategoriaAudit SELECT 'Update', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO CategoriaAudit SELECT 'Insert', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; 
    END;
$CategoriaAudit$ LANGUAGE plpgsql;

CREATE TRIGGER AuditoriaCategoria
AFTER INSERT OR UPDATE OR DELETE ON Categoria
FOR EACH ROW EXECUTE PROCEDURE funAuditarCategoria();

CREATE TABLE StatusAudit(
    operacion           char(6)   NOT NULL,
    fecha               timestamp NOT NULL,
    idUsuarioA          text      NOT NULL,
    idStatus		int,
    nombre              varchar(45)
);

CREATE OR REPLACE FUNCTION funAuditarStatus() RETURNS TRIGGER AS $StatusAudit$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO StatusAudit SELECT 'Delete', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO StatusAudit SELECT 'Update', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO StatusAudit SELECT 'Insert', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; 
    END;
$StatusAudit$ LANGUAGE plpgsql;

CREATE TRIGGER AuditoriaStatus
AFTER INSERT OR UPDATE OR DELETE ON Status
FOR EACH ROW EXECUTE PROCEDURE funAuditarStatus();

CREATE TABLE ArticuloAudit(
    operacion           char(6)   NOT NULL,
    fecha               timestamp NOT NULL,
    idUsuarioA          text      NOT NULL,
    idArticulo		int,
    nombre              varchar(45),
    url                 varchar(60),
    puntuacion          int,
    fechaIngreso        timestamp,
    descripcion         varchar (100),
    precio              float4,
    imagen              varchar(45),
    idCategoria         int,
    idStatus            int,
    idUsuario           int
);

CREATE OR REPLACE FUNCTION funAuditarArticulo() RETURNS TRIGGER AS $ArticuloAudit$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO ArticuloAudit SELECT 'Delete', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO ArticuloAudit SELECT 'Update', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO ArticuloAudit SELECT 'Insert', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; 
    END;
$ArticuloAudit$ LANGUAGE plpgsql;

CREATE TRIGGER AuditoriaArticulo
AFTER INSERT OR UPDATE OR DELETE ON Articulo
FOR EACH ROW EXECUTE PROCEDURE funAuditarArticulo();

CREATE TABLE SubastaAudit(
    operacion           char(6)   NOT NULL,
    fecha               timestamp NOT NULL,
    idUsuarioA          text      NOT NULL,
    idSubasta		int,
    fechaInicio         timestamp,
    valorInicial        float4,
    fechaExpira         timestamp,
    status              varchar(45),
    idArticulo          int
);

CREATE OR REPLACE FUNCTION funAuditarSubasta() RETURNS TRIGGER AS $SubastaAudit$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO SubastaAudit SELECT 'Delete', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO SubastaAudit SELECT 'Update', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO SubastaAudit SELECT 'Insert', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; 
    END;
$SubastaAudit$ LANGUAGE plpgsql;

CREATE TRIGGER AuditoriaSubasta
AFTER INSERT OR UPDATE OR DELETE ON Subasta
FOR EACH ROW EXECUTE PROCEDURE funAuditarSubasta();

CREATE TABLE OfertaAudit(
    operacion           char(6)   NOT NULL,
    fechaA              timestamp NOT NULL,
    idUsuarioA          text      NOT NULL,
    idOferta		int,
    valor               float4,
    fecha               timestamp,
    idSubasta           int,
    idUsuario           int 
);

CREATE OR REPLACE FUNCTION funAuditarOferta() RETURNS TRIGGER AS $OfertaAudit$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO OfertaAudit SELECT 'Delete', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO OfertaAudit SELECT 'Update', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO OfertaAudit SELECT 'Insert', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; 
    END;
$OfertaAudit$ LANGUAGE plpgsql;

CREATE TRIGGER AuditoriaOferta
AFTER INSERT OR UPDATE OR DELETE ON Oferta
FOR EACH ROW EXECUTE PROCEDURE funAuditarOferta();

CREATE TABLE VisitaAudit(
    operacion           char(6)   NOT NULL,
    fechaA              timestamp NOT NULL,
    idUsuarioA          text      NOT NULL,
    idVisita		int,
    fecha               timestamp,
    idArticulo          int,
    idUsuario           int 
);

CREATE OR REPLACE FUNCTION funAuditarVisita() RETURNS TRIGGER AS $VisitaAudit$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO VisitaAudit SELECT 'Delete', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO VisitaAudit SELECT 'Update', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO VisitaAudit SELECT 'Insert', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; 
    END;
$VisitaAudit$ LANGUAGE plpgsql;

CREATE TRIGGER AuditoriaVisita
AFTER INSERT OR UPDATE OR DELETE ON Visita
FOR EACH ROW EXECUTE PROCEDURE funAuditarVisita();

CREATE TABLE DetalleAudit(
    operacion           char(6)   NOT NULL,
    fecha               timestamp NOT NULL,
    idUsuarioA          text      NOT NULL,
    idDetalle		int,
    cantidad            int,
    descuento           float4,
    idPedido            int,
    idArticulo          int
);

CREATE OR REPLACE FUNCTION funAuditarDetalle() RETURNS TRIGGER AS $DetalleAudit$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO DetalleAudit SELECT 'Delete', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO DetalleAudit SELECT 'Update', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO DetalleAudit SELECT 'Insert', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; 
    END;
$DetalleAudit$ LANGUAGE plpgsql;

CREATE TRIGGER AuditoriaDetalle
AFTER INSERT OR UPDATE OR DELETE ON Detalle
FOR EACH ROW EXECUTE PROCEDURE funAuditarDetalle();

CREATE TABLE RevisionAudit(
    operacion           char(6)   NOT NULL,
    fechaA              timestamp NOT NULL,
    idUsuarioA          text      NOT NULL,
    idRevision		int,
    fecha               timestamp,
    titulo              varchar(45),
    descripcion         varchar(100),
    estrellas           int,
    idArticulo          int,
    idUsuario           int 
);

CREATE OR REPLACE FUNCTION funAuditarRevision() RETURNS TRIGGER AS $RevisionAudit$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO RevisionAudit SELECT 'Delete', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO RevisionAudit SELECT 'Update', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO RevisionAudit SELECT 'Insert', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; 
    END;
$RevisionAudit$ LANGUAGE plpgsql;

CREATE TRIGGER AuditoriaRevision
AFTER INSERT OR UPDATE OR DELETE ON Revision
FOR EACH ROW EXECUTE PROCEDURE funAuditarRevision();


CREATE TABLE MetodoPagoAudit(
    operacion           char(6)   NOT NULL,
    fechaA              timestamp NOT NULL,
    idUsuarioA          text      NOT NULL,
    idMetodoPago	int,
    tipoPago            int,
    monto               float4,
    idUsuario           int 
);

CREATE OR REPLACE FUNCTION funAuditarMetodoPago() RETURNS TRIGGER AS $MetodoPagoAudit$
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO MetodoPagoAudit SELECT 'Delete', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO MetodoPagoAudit SELECT 'Update', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO MetodoPagoAudit SELECT 'Insert', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; 
    END;
$MetodoPagoAudit$ LANGUAGE plpgsql;

CREATE TRIGGER AuditoriaMetodoPago
AFTER INSERT OR UPDATE OR DELETE ON MetodoPago
FOR EACH ROW EXECUTE PROCEDURE funAuditarMetodoPago();